package com.sea.physical.service.impl;


import com.codecrane.core.web.BatchImportUtils;
import com.sea.physical.entity.StockCreatePlan;
import com.sea.physical.entity.StockCreateRecord;
import com.sea.physical.entity.StockCreateSku;
import com.sea.physical.entity.StockProduce;
import com.sea.physical.mapper.StockCreateSkuMapper;
import com.sea.physical.service.StockCreatePlanService;
import com.sea.physical.service.StockCreateRecordService;
import com.sea.physical.service.StockCreateSkuService;
import com.sea.physical.service.StockProduceService;
import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.util.PhysicalStockStatusEnum;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class StockCreateSkuServiceImpl implements StockCreateSkuService {

    @Autowired
    private StockCreatePlanService stockCreatePlanService;
    @Autowired
    private StockProduceService stockProduceService;
    @Autowired
    private StockCreateSkuMapper stockCreateSkuMapper;
    @Autowired
    private StockCreateRecordService stockCreateRecordService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName, Integer createId) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<StockCreateSku> list = BatchImportUtils.iteratorForStockCreateSku(sheet);
            if (null == list || list.size() == 0) {
                throw new RuntimeException("请至少新增一条明细！");
            } else {
                StockCreatePlan stockCreatePlan = stockCreatePlanService.findById(createId);
                for (StockCreateSku stockCreateSku : list) {
                    //重新做分配的明细
                    StockCreateSku newStockCreateSku = new StockCreateSku();
                    newStockCreateSku.setCreateId(createId);
                    newStockCreateSku.setCreateShipmentId(stockCreateSku.getCreateShipmentId());
                    newStockCreateSku.setCreatePackage(stockCreateSku.getCreatePackage());
                    newStockCreateSku.setCreateLength(stockCreateSku.getCreateLength());
                    newStockCreateSku.setCreateWidth(stockCreateSku.getCreateWidth());
                    newStockCreateSku.setCreateHeight(stockCreateSku.getCreateHeight());
                    //获取采购在产剩余量
                    Integer createWareId = stockCreatePlan.getCreateWareId();
                    String createSku = stockCreateSku.getCreateSku();
                    Integer createNumber = stockCreateSku.getCreateNumber();
                    //首次校验库存
                    Integer stockNumber = stockProduceService.getStockNumber(createWareId, createSku, createNumber);
                    if (null == stockNumber || stockNumber < 0) {
                        throw new RuntimeException(stockCreatePlan.getCreateWareName() + "库存不足,SKU:" + createSku);
                    } else {
                        //获取可占用库存
                        List<StockProduce> produceList = stockProduceService.getUseNumber(createWareId, createSku);
                        if (null == produceList || produceList.size() <= 0) {
                            throw new RuntimeException(stockCreatePlan.getCreateWareName() + "库存不足,SKU:" + createSku);
                        } else {
                            Integer number = createNumber;
                            for (StockProduce stockProduce : produceList) {
                                //赋值分配的明细
                                newStockCreateSku.setCreateProduceId(stockProduce.getId());
                                newStockCreateSku.setCreateNo(stockProduce.getStockNo());
                                newStockCreateSku.setCreateSku(stockProduce.getStockSku());
                                //获取可占用量
                                Integer count = stockProduce.getStockUseNumber() - stockProduce.getStockNumber();
                                createNumber = createNumber + count;
                                //如果等于0说明正好分完则不用管了
                                if (createNumber == 0) {
                                    stockProduceService.updateUseNumber(stockProduce.getStockNumber(), stockProduce.getId());
                                    newStockCreateSku.setCreateNumber(stockProduce.getStockNumber() - stockProduce.getStockUseNumber());
                                    newStockCreateSku.setCreateBoxNumber((double) (newStockCreateSku.getCreateNumber()) / (double) newStockCreateSku.getCreatePackage());
                                    newStockCreateSku.setCreateVolume(newStockCreateSku.getCreateBoxNumber() * stockCreateSku.getCreateVolume());
                                    save(newStockCreateSku);
                                    break;
                                }
                                if (createNumber > 0) {
                                    stockProduceService.updateUseNumber(stockProduce.getStockNumber(), stockProduce.getId());

                                    newStockCreateSku.setCreateNumber(stockProduce.getStockNumber() - stockProduce.getStockUseNumber());
                                    newStockCreateSku.setCreateBoxNumber((double) (newStockCreateSku.getCreateNumber()) / (double) newStockCreateSku.getCreatePackage());
                                    newStockCreateSku.setCreateVolume(newStockCreateSku.getCreateBoxNumber() * stockCreateSku.getCreateVolume());
                                    save(newStockCreateSku);
                                    continue;
                                }
                                //如果小于0说明不够分了
                                if (createNumber < 0) {
                                    stockProduceService.updateUseNumber(stockProduce.getStockNumber() + createNumber, stockProduce.getId());
                                    newStockCreateSku.setCreateNumber(stockProduce.getStockNumber() + createNumber - stockProduce.getStockUseNumber());
                                    newStockCreateSku.setCreateBoxNumber(((double) (newStockCreateSku.getCreateNumber())) / (double) newStockCreateSku.getCreatePackage());
                                    newStockCreateSku.setCreateVolume(newStockCreateSku.getCreateBoxNumber() * stockCreateSku.getCreateVolume());
                                    save(newStockCreateSku);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        //更新表头
        stockCreatePlanService.updateStockCreatePlan(createId);
    }

    @Override
    public int save(StockCreateSku stockCreateSku) {
        return stockCreateSkuMapper.insert(stockCreateSku);
    }

    @Override
    public List<StockCreateSku> findByCreateId(Integer createId) {
        return stockCreateSkuMapper.findByCreateId(createId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void send(StockCreateRecord stockCreateRecord) throws Exception {
        List<StockCreateSku> list = stockCreateSkuMapper.findByCreateId(stockCreateRecord.getCreateId());
        if(null==list || list.size()==0){
            throw new RuntimeException("发运异常,请联系管理员！");
        }else {
            for(StockCreateSku stockCreateSku:list){
                int i = stockProduceService.updateStockNumber(stockCreateSku.getCreateNumber(), stockCreateSku.getCreateProduceId());
                if(i==0){
                    throw new Exception("发运异常,请联系管理员！");
                }else{
                    StockCreatePlan stockCreatePlan=new StockCreatePlan();
                    stockCreatePlan.setCreateStatus(2);
                    stockCreatePlan.setId(stockCreateRecord.getCreateId());
                    stockCreatePlanService.usave(stockCreatePlan);
                }
            }


            //获取操作人信息
            stockCreateRecord.setCreateId(stockCreateRecord.getCreateId());
            Subject subject = SecurityUtils.getSubject();
            PowerUser user = (PowerUser) subject.getSession().getAttribute(Constants.CURRENT_USER_SESSION);
            if (null != user) {
                stockCreateRecord.setCreateOperateName(user.getUserNickname());
            }else{
                throw new Exception("暂未获取到用户信息");
            }
            stockCreateRecord.setCreateStatus(2);
            stockCreateRecord.setCreateRemark(PhysicalStockStatusEnum.getStatusName(2));
            stockCreateRecordService.save(stockCreateRecord);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cancel(Integer createId) {
        List<StockCreateSku> skuList = stockCreateSkuMapper.findByCreateId(createId);
        if(null!=skuList && skuList.size()!=0){
            for(StockCreateSku sku:skuList){
                stockProduceService.cancelUseNumber(sku.getCreateNumber(),sku.getCreateProduceId());
                stockCreateSkuMapper.delete(sku.getId());
            }
            stockCreatePlanService.delete(createId);
        }else{
            stockCreatePlanService.delete(createId);
        }
    }

    @Override
    public List<StockCreateSku> export(StockCreateSku stockCreateSku) {
        return stockCreateSkuMapper.export(stockCreateSku);
    }

    @Override
    public List<StockCreateSku> findSalesPhysicalNumber(StockCreateSku stockCreateSku) {
        return stockCreateSkuMapper.findSalesPhysicalNumber(stockCreateSku);
    }
}
